﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Infrastructures;
using Models;
using ConcreteService;

namespace SponsorshipWeb.personal
{
    public partial class publish2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //var user = AuthUtil.GetAuthentication();
                //if (user == null)
                //    Response.Redirect("../login.html");

                //var id = Request.QueryString["id"];
                //if (id.IsNullOrEmpty())
                //    Response.Redirect("publish1.html");

                //hfUserID.Value = user.UserID.ToString();
                //hfProjectID.Value = id;

                //string mode = Request.QueryString["mode"];
                //if (mode == "edit")
                //{
                //    hfMode.Value = mode;
                //    BindUI(id);
                //}
                UtilService utilService = ServiceFactory.CreateInstance<UtilService>();
                utilService.SpliteWord(SpliteWordFun.School);
            }
        }

        private void BindUI(string id)
        {
            //IProjectService service = ServiceFactory.CreateInstance<IProjectService>();
            //var project = service.Get<Project>(id.ToString());

            //IUtilService utilService = ServiceFactory.CreateInstance<IUtilService>();
            //var lst = utilService.ListFileSystem(project.ProjectNO);

            //txtRemark.Text = project.ProjectRequire;
        }


        protected void btnNext_Click(object sender, EventArgs e)
        {
            //string strError = string.Empty;

            //if (string.IsNullOrWhiteSpace(hfMode.Value))
            //{
            //    if (fuBook.HasFile == false || String.IsNullOrEmpty(fuBook.PostedFile.FileName) ||
            //        fuImg1.HasFile == false || String.IsNullOrEmpty(fuImg1.PostedFile.FileName) ||
            //        fuImg2.HasFile == false || String.IsNullOrEmpty(fuImg2.PostedFile.FileName) ||
            //        fuImg3.HasFile == false || String.IsNullOrEmpty(fuImg3.PostedFile.FileName))
            //    {
            //        strError = "ParseExcel:: No file found, please input a file.";
            //        return;
            //    }
            //}

            //IProjectService service = ServiceFactory.CreateInstance<IProjectService>();
            //var project = service.Get<Project>(hfProjectID.Value);

            //project.ProjectRequire = txtRemark.Text;

            //service.Update(project, hfUserID.Value);

            //IList<FileSystem> lst = null;

            //if (hfMode.Value == "edit" && (fuBook.HasFile || fuImg1.HasFile || fuImg2.HasFile || fuImg3.HasFile))
            //{
            //    IUtilService utilService = ServiceFactory.CreateInstance<IUtilService>();
            //    lst = utilService.ListFileSystem(hfProjectID.Value.ToInt());
            //}

            //SaveFile(fuBook, FileType.SponsorBook, lst, project.ProjectNO);
            //SaveFile(fuImg1, FileType.ActImg1, lst, project.ProjectNO);
            //SaveFile(fuImg2, FileType.ActImg2, lst, project.ProjectNO);
            //SaveFile(fuImg3, FileType.ActImg3, lst, project.ProjectNO);

            //Response.Redirect("myActivity.html");
        }

        private void SaveFile(FileUpload fu, FileType type, IList<FileSystem> lst, int projectNo)
        {
            //if (!fu.HasFile)
            //    return;

            //string fileName = fu.FileName;
            //string fileExt = Path.GetExtension(fileName);
            //string strDir = SystemConfig.Get("UploadPath") + "Student\\" + hfUserID.Value + "\\" + projectNo + "\\";

            //if (!Directory.Exists(strDir))
            //    Directory.CreateDirectory(strDir);

            //string strSysName = type.ToString() + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt;
            //fu.SaveAs(strDir + strSysName);

            //FileSystem fs = null;

            //if (lst != null && lst.Count > 0)
            //    fs = lst.FirstOrDefault(x => x.Fun == type);
            //else
            //    fs = new FileSystem();

            //fs.Fun = type;
            //fs.TypeName = APPRole.Project;
            //fs.Belong = projectNo;
            //fs.NewName = strSysName;
            //fs.OldName = fileName;
            //fs.Path = SystemConfig.Get("UploadHttpPath") + "Student/" + hfUserID.Value + "/" + projectNo + "/" + strSysName;
            //fs.Status = CommonStatus.Active;
            //fs.InstanceID = SystemConfig.Get("InstanceID");
            //fs.CreateUser = hfUserID.Value.ToInt();

            //IUtilService service = FactoryCreator.Create<IUtilService>();

            //if (lst == null || lst.Count == 0)
            //    service.CreateFile(fs);
            //else
            //    service.UpdateFile(fs);
        }
    }
}